package io.renren.modules.news.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.news.entity.ChannelEntity;
import io.renren.modules.news.service.ChannelService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;


/**
 * 
 * 
 * @author wxy
 * @email sharonwang_xy@163.com
 * @date 2018-10-16 00:55:58
 */
@RestController
@RequestMapping("news/channel")
public class ChannelController {
	@Autowired
	private ChannelService channelService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("news:channel:list")
	public R list(@RequestParam Map<String, Object> params){

		// 防止查询下拉列表报错
		params.putIfAbsent("sidx", "");
		params.putIfAbsent("order", "asc");

		//查询列表数据
        Query query = new Query(params);

		List<ChannelEntity> channelList = channelService.queryList(query);
		int total = channelService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(channelList, total, query.getLimit(), query.getCurrPage());
		
		return R.ok().put("page", pageUtil);
	}

	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("news:channel:info")
	public R info(@PathVariable("id") Integer id){
		ChannelEntity channel = channelService.queryObject(id);
		
		return R.ok().put("channel", channel);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("news:channel:save")
	public R save(@RequestBody ChannelEntity channel){
		channelService.save(channel);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("news:channel:update")
	public R update(@RequestBody ChannelEntity channel){
		channelService.update(channel);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("news:channel:delete")
	public R delete(@RequestBody Integer[] ids){
		channelService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
